﻿<?php
function umri($str){
	die($str."<br /><a href=\"javascript:history.back();\">nazad</a>");
}
function parseTemplate($file){
	$file = file("templates/".$file);
	return implode("",$file);
}
function genOptions($l,$start=1,$var=NULL){
	for($i=$start-1; $i<$l; $i++){
		if($var){
			echo "<option value=\"".($i+1)."\">".$var[$i]."</option>\n";
		}else{
			echo "<option>".($i+1)."</option>\n";
		}
	}
}

function start(){
$months = array("januar","februar","mart","april","maj","jun","jul","avgust","septembar","oktobar","novembar","decembar");
?>
<style>
label{
	cursor:pointer;
}
</style>
<form name="forma" method="post" action="">
	<fieldset style="float:left;">
	  <legend>OD:</legend>
	  	Dan:
		<select name="od_dan">
		<?php
			genOptions(31);
		?>
		</select>
		Mesec:
		<select name="od_mesec">
		<?php
			genOptions(12,1,$months);
		?>
		</select>
		Godina:
		<select name="od_godina">
		<?php
			genOptions(2006,2004);
		?>
		</select>
	</fieldset>
	<fieldset style="float:left;">
	  <legend>DO:</legend>
	  	Dan:
		<select name="do_dan">
		<?php
			genOptions(31);
		?>
		</select>
		Mesec:
		<select name="do_mesec">
		<?php
			genOptions(12,1,$months);
		?>
		</select>
		Godina:
		<select name="do_godina">
		<?php
			genOptions(2006,2004);
		?>
		</select>
	</fieldset>
	<div style="float:left;">
		<label for="preduzece">Preduzeće: </label><input type="text" name="preduzece" id="preduzece" size="5" />
		<br />
		<input type="submit" value="create" style="margin:10px 0px 0px 50px;" />
		<input type="hidden" name="action" value="1" />
	</div>
</form>
<?php
}

class template{
	var $html;
	//array u kome se skupljaju razne promenljive i drugi arrayi
	var $_arej;
	//string u kojem je adresa do templates direktorijuma
	var $_templates = "templates";
	//associative array u kome je config uchitan
	var $_configArej;
	//array count
	var $_arrcount;
	//array counter
	var $_arrcounter;
	//array u koji se smesta ceo template
	var $tmpArray;
	//pocetak loop-a
	var $loopstart;
	//duzina loop-a
	var $cyclenum;
	//loop counter
	var $loopcount;
	//output string
	var $output;
	//da li je prazan loop
	var $emptyloop;
	
	function template(){
	}
	function display(){
		echo $this->html;
	}
	function assign($ime,$vrednost){
		$this->_arej[$ime] = $vrednost;
		return true;
	}
	function parseTemplate($templateName,$slice = "/{{(.*?)}}/is"){
		$this->tmpArray = $this->getTemplate($templateName);
		$output = implode ("", $this->tmpArray);
		$this->tmpArray = preg_split($slice, $output, -1, PREG_SPLIT_DELIM_CAPTURE);
		$this->_arrcount = count($this->tmpArray);
		$this->_arrcounter = 0;
		$output = "";
		while ($this->_arrcounter < $this->_arrcount) {
			$linija = trim ($this->tmpArray[$this->_arrcounter]);
			if ($out = $this->isTag($linija)) {
				if ($this->emptyloop) {
					$out = " ";
				}
				if ($out == " ") {
					$out = "\n";
				}
				$output .= $out;
			}elseif (!$this->emptyloop){
				$output .= $linija;
			}
			$this->_arrcounter ++;
		}
		return $output;
	}
	function getTemplate($name){
		return file($this->_templates."/".$name);
	}
	function isTag($lin){
		switch(substr($lin,0,1)){
			case "#": // rad sa configom
				$tempout = substr(strrchr ($lin, "#"), 1);
				$return = $this->getFromConfig($tempout);
				break;
			case "$": // rad sa promenljivama
				$tempout = substr(strrchr ($lin, "$"), 1);
				if (is_array($this->_arej[$tempout])){
					if ($this->_arej[$tempout][$this->loopcount] != NULL) {
						$return = $this->_arej[$tempout][$this->loopcount];
					}else{
						$return = " ";
					}
				}else{
					if ($this->_arej[$tempout] != "") {
						$return = $this->_arej[$tempout];
					}else{
						$return = " ";
					}
				}
				break;
			case "%": // rad sa funkcijama
				$tempout = substr(strrchr ($lin, "%"), 1);
				$return = $this->callFunction($tempout);
				break;
			default: // ostalo
				$return = false;
				break;
		}
		return $return;
	}
	function callFunction ($linija){
		$cmdstr = explode(" ", $linija);
		$prvi = true;
		foreach ($cmdstr as $deo) {
			if ($deo != "") {
				if ($prvi) {
					$komanda = $deo;
					$prvi = false;
				}else{
					$parametri[] = $deo;
				}
			}
		}
		switch ($komanda) {
			case "loop":
				return $this->loop($parametri);
				break;
			case "loopnum":
				return $this->loopnum($parametri);
				break;
			case "config":
				return $this->addConfig($parametri);
				break;
			case "random":
				return $this->random();
				break;
			default:
				return " ";
		}
	}
	function loop($param) {
		$out = "";
		if ($param[0] == "start") {
			$this->loopstart = ($this->getCounter());
			$this->cyclenum = count($this->_arej[$param[1]]);
			$this->loopcount = 0;
			if (!$this->cyclenum) {
				$this->emptyloop = true;
			}
			return " ";
		}
		if ($param[0] == "end") {
			$this->emptyloop = false;
			$this->loopcount ++;
			if ($this->loopcount < $this->cyclenum) {
				$this->_arrcounter = $this->loopstart;
			}
			return " ";
		}
		return false;
	}
	function loopnum(){
		return $this->loopcount + 1;
	}
	function getCount() {
		return $this->_arrcount;
	}
	function getCounter() {
		return $this->_arrcounter;
	}
}
?>